package jwdt.common.dao.impl;

import jwdt.common.dao.BaseDao;
import jwdt.common.util.NewObjectUtil;

import com.ezerg.access.MethodAccess;
import com.ezerg.beanutils.BeanUtils;
import com.ezerg.db.Jdb;
import com.ezerg.db.mapping.DbMap;
import com.ezerg.util.StringUtil;
import com.ezerg.web.engine.WebCache;

@SuppressWarnings({ "unchecked", "rawtypes" })
public class BaseDaoImpl<T> implements BaseDao<T> {
	
	@Override
	public String doAddObj(T obj) throws Exception{
		String ret = "添加成功";
		try {
			Jdb.get().add(obj);
		} catch (Exception e) {
			e.printStackTrace();
			ret = "添加出错";
		}
		return ret;
	}
	
	@Override
	public String doDelObj(Class clz,Object id) throws Exception {
		Jdb jdb = Jdb.get();
		String ret = "删除成功";
		T obj = (T)jdb.get(clz, id);
		if(obj == null) {
			ret="您要删除的记录不存在";
			return ret;
		}
		try {
			jdb.del(obj);
		} catch (Exception e) {
			e.printStackTrace();
			ret = "删除出错";
		}
		return ret;
	}
	
	@Override
	public String doUpdateObj(Class clz,Object id,T obj)throws Exception{
		Jdb jdb = Jdb.get();
		String ret ="修改成功";
		T t = (T)jdb.get(clz, id);
		if(t == null) {
			ret ="您要修改的记录不存在";
			return ret;
		}
		try {
			jdb.update(obj);
		} catch(Exception e) {
			e.printStackTrace();
			ret ="修改出错";
		}
		return ret;
	}

	@Override
	public String doUpdateExactlyObj(Class clz,Object id,T obj)throws Exception{
		Jdb jdb = Jdb.get();
		String ret ="修改成功";
		T t = (T)jdb.get(clz, id);
		if(t == null) {
			ret ="您要修改的记录不存在";
			return ret;
		}
		try {
			jdb.updateExactly(obj, t);
		} catch(Exception e) {
			e.printStackTrace();
			ret ="修改出错";
		}
		return ret;
	}

	@Override
	public T doLoadObj(Class clz, int id) throws Exception {
		T obj;
		if(id != 0){
			obj = (T)Jdb.get().get(clz, id);
		}else{
			obj = (T)NewObjectUtil.getNewObject(clz);
		}
		return obj;
	}

	@Override
	public T doLoadObj(Class clz, Object id) throws Exception {
		T obj;
		if(id != null){
			obj = (T)Jdb.get().get(clz, id);
		}else{
			obj = (T)NewObjectUtil.getNewObject(clz);
		}
		return obj;
	}

	@Override
	public String doIntUpdateState(Class clz, String querySql, String setSql)
			throws Exception {
		Jdb jdb = Jdb.get();
		int i = querySql.indexOf("=");
		int j = setSql.indexOf("=");
		String ret = "参数格式出错！请传递正确的参数格式如：id=1";
		if(i == -1 || j == -1){
			return ret;
		}
		String id = querySql.substring(i+1).replace("'", "");
		T obj = (T) jdb.get(clz,id);
		if(obj == null) {
			ret ="您要操作的记录不存在";
			return ret;
		}
		String methodName = setSql.substring(0,j);
		String value = setSql.substring(j+1);
		String mehtod = "set"+methodName.substring(0, 1).toUpperCase()+methodName.substring(1);
		String key = (new StringBuilder("MethodAccess:")).append(clz).toString();
		MethodAccess ma = (MethodAccess)WebCache.get(key);
	    if(ma == null) {
	        ma = MethodAccess.get(clz);
	        WebCache.put(key, ma);
	    }
	    String tableName = DbMap.get().findTableByClass(clz).getName();
	    String sql = "update "+tableName+" set "+setSql+" where " + querySql;
		try {
			ma.invoke(obj, mehtod,new Object[]{StringUtil.parseInt(value)});
			jdb.execute(sql,false);
			ret = "操作成功";
		} catch (Exception e) {
			e.printStackTrace();
			ret = "操作出错";
		}
		return ret;
	}
	
	@Override
	public String doUpdateState(Class clz, String querySql, String setSql)
			throws Exception {
		Jdb jdb = Jdb.get();
		int i = querySql.indexOf("=");
		int j = setSql.indexOf("=");
		String ret = "参数格式出错！请传递正确的参数格式如：id=1";
		if(i == -1 || j == -1){
			return ret;
		}
		String id = querySql.substring(i+1).replace("'", "");
		T obj = (T) jdb.get(clz,id);
		if(obj == null) {
			ret ="您要操作的记录不存在";
			return ret;
		}
		String methodName = setSql.substring(0,j);
		String value = setSql.substring(j+1);
	    String tableName = DbMap.get().findTableByClass(clz).getName();
	    String sql = "update "+tableName+" set "+setSql+" where " + querySql;
		try {
			BeanUtils.setProperty(obj, methodName, value);
			jdb.execute(sql,false);
			ret = "操作成功";
		} catch (Exception e) {
			e.printStackTrace();
			ret = "操作出错";
		}
		return ret;
	}
}
